<?php
/**
 * 支付
 */

namespace app\controller\v1;


use app\model\Bank;
use app\model\UserDataModel;
use app\Request;
use app\services\AdminPayServices;
use app\services\AliPayServices;
use app\services\balancePayServices;
use app\services\GongmallServices;
use app\services\OfflinePayServices;
use app\services\SendCode;
use app\services\WxPayServices;
use think\facade\Db;
use utils\AES;
use utils\AliPay;
use utils\WxPay;

class Pay
{
    /**
     * 支付下单
     * @param Request $request
     * @return mixed
     */
    public function payment(Request $request)
    {
        RedisLock('userPay_' . $request->user_id);
        try {
            $channel = $request->data['channel']; // 支付方式 1 支付宝支付 2 微信支付 3 余额
            switch ($channel) {
                case 1:
                    return AliPayServices::instance()->userId($request->user_id)->data($request)->getUnifiedOrder();
                    break;
                case 2:
                    return WxPayServices::instance()->userId($request->user_id)->data($request->data)->getUnifiedOrder();
                    break;
                case 3:
                    security_password_verify($request);
                    return balancePayServices::instance()->userId($request->user_id)->data($request)->getUnifiedOrder();
                    break;
            }
        } catch (\Throwable $e) {
            return app('json')->json_error($e->getMessage());
        }
    }

    /**
     * 交易订单状态查询
     * @param string $trade_no 订单编号
     * @param $channel 1 支付宝支付 2 微信支付 4.线下交易
     * @param int $offline_pay_way
     * @return bool
     * @throws \Exception
     */
    public function tradeOrderStatus(string $trade_no, int $channel, int $offline_pay_way = 0)
    {
        switch ($channel) {
            case 1:
                /** @var AliPay $pay */
                $pay = app()->make(Alipay::class);
                return $pay->tradeQuery($trade_no);
                break;
            case 2:
                /** @var WxPay $pay */
                $pay = app()->make(WxPay::class);
                return $pay->tradeQuery($trade_no);
                break;
            case 4: // 线下交易
                if ($offline_pay_way == 1) { // 支付宝
                    /** @var AliPay $pay */
                    $pay = app()->make(Alipay::class, ['offline']);
                } else { // 微信
                    /** @var WxPay $pay */
                    $pay = app()->make(WxPay::class);
                }
                return $pay->tradeQueryOffline($trade_no);
                break;
            default:
                return false;
        }
    }

    /**
     * 支付宝支付回调
     * @param Request $request
     * @return mixed
     */
    public function aliResult(Request $request)
    {
        file_put_contents('/www/wwwroot/poly-select-php-interface/alipay.txt', var_export($request->param(), true) . '--------' . date('H:i:s') . PHP_EOL, FILE_APPEND);
        return AliPayServices::instance()->data($request->param())->getNotifyInfo()->afterPay();
    }

    /**
     * 微信支付回调
     * @param Request $request
     * @return mixed
     */
    public function wxResult(Request $request)
    {
        file_put_contents('/www/wwwroot/poly-select-php-interface/wechat.txt', var_export($request->param(), true) . '--------' . date('H:i:s') . PHP_EOL, FILE_APPEND);
        return WxPayServices::instance()->data($request->param())->getNotifyInfo()->afterPay();
    }


    /**
     * 银行卡操作
     */
    public function BankList(Request $request)
    {
        try {
            $result = Db::name('bank')->where(['user_id'=>$request->user_id])->paginate(['page'=>1,'limit'=>100])->toArray();
            return app('json')->json_success('成功',$result);
        } catch (\Exception $e) {
            return app('json')->json_error($e->getMessage());
        }
    }

    /**
     * 银行卡操作
     */
    public function BankModify(Request $request)
    {
        try {
            if ($request->user->is_real_name != 1) {
                return app('json')->json_error('请先完成实名认证');
            }
            if ($request->data['cls'] == 1) {
                $model = new Bank();
            } else {
                app()->make(SendCode::class)->CodeVerify($request->data['code'], $request->user->phone, 2);
                $model = Bank::where(['id' => $request->data['id'], 'user_id' => $request->user_id, 'type' =>  $request->data['type']])->find();

                if (!$model) {
                    return app('json')->json_error('不存在');
                }
            }
            $model->save($request->data);
            return app('json')->json_success('成功');
        } catch (\Exception $e) {
            return app('json')->json_error($e->getMessage());
        }
    }


    /**
     * 获取支付设置
     * @return mixed
     */
    public function paySetting()
    {
        $result = [];
        try {
            $result['pay_way'] = get_config(2, 'PAYTYPE');
            $result['offline_pay'] = get_config(2, 'OFFLINEPAY');
        } catch (\Exception $e) {

        }
        return app('json')->json_success('获取成功', $result);
    }


}